package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.itheima.model.domain.User;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.mongo.Friend;
import com.itheima.model.tanhua.sso.vo.ErrorResult;
import com.itheima.model.vo.ContactVo;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.UserInfoVo;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.template.HuanXinTemplate;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @Autowired
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 根据环信用户id查询用户详情
     *
     * @param huanxinId
     * @return
     */
    public UserInfoVo findUserInfoHuanXin(String huanxinId) {
        //1、根据环信id查询用户
        User user = userApi.findByHuanXin(huanxinId);
        //2、根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //构建vo返回
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo); //copy同名同类型的属性

        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    /**
     * 添加好友
     */
    public void contacts(Long friendId) {
        //1、将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);

        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }

        //2、如果注册成功，将好友关系记录到MongoDB
        friendApi.save(UserHolder.getUserId(),friendId);
    }

    /**
     * 删除好友
     */
    public void delete(Long friendId) {
        //1、将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);

        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }

        //2、如果注册成功，将好友关系记录到MongoDB
        friendApi.save(UserHolder.getUserId(),friendId);
    }

    /**
     * 查询好友列表
     * @param keyword
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult findFriendlist(String keyword, Integer page, Integer pagesize) {
        //1、调用API查询当前用户的好友数据
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //2、提取数据列表中的好友数据
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3、调用UserInfoAPI查询好友的用户详情
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, userInfo);
        //4、构造VO对象
        ArrayList<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo info = map.get(friend.getFriendId());
            if (userInfo != null) {
                ContactVo vo = ContactVo.init(info);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0L,vos);
    }
}
